package com.mywang.weitravel.modules.mine;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mywang.weitravel.R;
import com.mywang.weitravel.base.BaseActivity;
import com.mywang.weitravel.base.MyApplication;
import com.mywang.weitravel.common.Contacts;
import com.mywang.weitravel.model.UserBean;
import com.mywang.weitravel.util.PreferencesUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class UserInfoActivity extends BaseActivity<UserInfoPresenter> implements UserInfoContract.View {

    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, UserInfoActivity.class);
        launchActivity(activity, intent);
    }

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_user_name)
    EditText etUserName;


    @Override
    protected void initView() {
        tvTitle.setText("用户信息");
        Object userInfo = PreferencesUtil.getInstance(MyApplication.getApplication()).getObject(Contacts.SP_USER_INFO);
        if (userInfo instanceof UserBean && PreferencesUtil.getInstance(MyApplication.getApplication()).isLogin()) {
            String username = ((UserBean) userInfo).getUsername();
            if (!TextUtils.isEmpty(username)) {
                etUserName.setText(username);
                etUserName.setSelection(username.length());
            }
        }
    }


    @OnClick({R.id.iv_title_back, R.id.btn_change_user_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_back:
                finish();
                break;

            case R.id.btn_change_user_name:
                String userName = etUserName.getText().toString();
                if (TextUtils.isEmpty(userName.trim())) {
                    ToastUtils.showShort("请输入昵称");
                } else {
                    mPresenter.changeUserName(userName);
                }
                break;
        }
    }

    @Override
    protected UserInfoPresenter initPresenter() {
        return new UserInfoPresenter(this);
    }

    @Override
    protected int getActivityLayoutID() {
        return R.layout.activity_user_info;
    }

    @Override
    public void changeUserNameSuccessful() {
        ToastUtils.showShort("修改昵称成功");
        finish();
    }
}
